public class Solution {
    public int lengthOfLongestSubstring(String S) {
        char[] s = S.toCharArray();
        int n = s.length;
        int ans = 0;
        int left = 0;
        int[] cnt = new int[128];
        for (int right = 0; right < n; right++) {
            char c = s[right];
            cnt[c]++;
            while (cnt[c] > 1) {
                cnt[s[left]]--;
                left++;
            }
            ans = Math.max(ans, right - left + 1);
        }
        return ans;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        String S = "pwwkew";
        System.out.println(solution.lengthOfLongestSubstring(S));
    }
}
